/**
 * Created by Administrator on 2016/1/5.
 */
angular.module('bluetoothoptions.controller',[])
  .controller('blueToothOptionsCtrl',['$scope','$q', '$ionicPopup', '$ionicLoading','$localstorage','$state','blueToothOptionsService',function($scope,$q,$ionicPopup,$ionicLoading,$localstorage,$state,blueToothOptionsService){

    $scope.isEnabled=function(){
      blueToothOptionsService.isEnabled().then(function(){
        $scope.bluetoothStatus = blueToothOptionsService.getBleStatus();
      });
    }
    $scope.searchBlueToothDevices = function () {
      //弹出加载动画
      $ionicLoading.show({
        template: '<ion-spinner icon="android"></ion-spinner>',
        content: '拼命加载中，请耐心等候...',
        animation: 'fade-in',
        showBackdrop: false,
        maxWidth: 200,
        showDelay: 0
      });

      var promises = blueToothOptionsService.search();
      //alert(promises);
      $q.all(promises).then(function () {
        $scope.bluetoothdevices = blueToothOptionsService.all();
        //alert(bluetoothdevices.length);
        //关闭加载动画
        $ionicLoading.hide();
      });

    };
    //设置默认连接的蓝牙读卡器
    $scope.setDefaultDevice = function (_name, _mac) {
      //alert(_name+_mac);
      //  confirm 对话框
      var confirmPopup = $ionicPopup.confirm({
        title: '设置确认',
        template: '确认将'+ _name + '设为默认连接蓝牙读卡器吗？',
        buttons: [
          { text: '取消' },
          {text: '保存',
            type: 'button-positive',
            onTap: function(e){return true;}
          }
        ]

      });
      confirmPopup.then(function(res) {
        //alert("res="+res);
        if(res) {
          $localstorage.set('name', _name);
          $localstorage.set('mac', _mac);
        	//alert($localstorage.get('name')+"---"+$localstorage.get('mac'));
          blueToothOptionsService.setDefaultDevice(_name, _mac);
          $state.go("maincontent");
        } else {

        }
      });

    };

  }]);
